﻿using AutoMapper;
using Furion.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Tls.Crypto;
using XinGuanERP.Db;
using XinGuanERP.Models.Po.business;
using XinGuanERP.Models.Vo.business;
using XinGuanERP.Services.business.IServices;
using XinGuanERP.Utils;

namespace XinGuanERP.Services.business;

public class SuppilerService : ISuppilerService, ITransient
{
    private readonly DefaultDbContext _defaultDbContext;
    public SuppilerService(DefaultDbContext defaultDbContext)
    {
        _defaultDbContext = defaultDbContext;
    }
    
    public bool AddSuppiler(SuppilerVo suppilerVo)
    {
        var province = suppilerVo.Address[0];
        var city = suppilerVo.Address[1];
        var area = suppilerVo.Address[2];
        var address = province + "/" + city + "/" + area;
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<SuppilerVo, Suppiler>()
                .AfterMap((src, des) => des.Address = address)
                .BeforeMap((src, des) => src.Id = StrUtil.GUID())
                .AfterMap((src, des) => des.CreateTime = DateTime.Now)
                .BeforeMap((src, des) => des.CreateUserId = SecurityUtil.FindClaimValue("UserId"))
                .BeforeMap((src, des) => des.CreateUserName = SecurityUtil.FindClaimValue("UserName"));
        });
        var mapper = config.CreateMapper();
        _defaultDbContext.Suppilers.Add(mapper.Map<Suppiler>(suppilerVo));
        return _defaultDbContext.SaveChanges() > 0;
    }

    public bool UpdateSuppiler(SuppilerVo suppilerVo)
    {
        var res = _defaultDbContext.Suppilers.FirstOrDefault(t => t.Id == suppilerVo.Id);
        if (res == null) return false;
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<SuppilerVo, Suppiler>()
                .AfterMap((src, des) => des.Address = src.Address[0] + "/" + src.Address[1] + "/" + src.Address[2])
                .BeforeMap((src, des) => des.UpdateTime = DateTime.Now)
                .BeforeMap((src, des) => des.UpdateUserId = SecurityUtil.FindClaimValue("UserId"))
                .BeforeMap((src, des) => des.UpdateUserName = SecurityUtil.FindClaimValue("UserName"));
        });
        var mapper = config.CreateMapper();
        mapper.Map(suppilerVo, res);
        _defaultDbContext.Suppilers.Update(res);
        return _defaultDbContext.SaveChanges() > 0;
    }

    public bool DeleteSuppiler(string id)
    {
        var res = _defaultDbContext.Suppilers.FirstOrDefault(t => t.Id == id);
        if (res == null) return false;
        _defaultDbContext.Suppilers.Remove(res);
        return _defaultDbContext.SaveChanges() > 0;
    }

    public PageResult<SuppilerVo> GetSuppilerWithPage(SuppilerVo suppilerVo)
    {
        var page = suppilerVo.page;
        var pageSize = suppilerVo.pageSize;
        var res = new List<Suppiler>();
        var count = 0;
        var sql = _defaultDbContext.Suppilers.AsQueryable();
        if (!string.IsNullOrWhiteSpace(suppilerVo.Phone) && suppilerVo.Address == null)
        {
            res = sql.Where(t => 
                    EF.Functions.Like(t.Phone, "%" + suppilerVo.Phone + "%"))
                .Skip((page - 1) * pageSize).Take(pageSize).ToList();
            count = sql.Where(t =>
                EF.Functions.Like(t.Phone, "%" + suppilerVo.Phone + "%")).Count();
        }
        else if (suppilerVo.Address != null && string.IsNullOrWhiteSpace(suppilerVo.Phone))
        {
            res = sql.Where(t => 
                    EF.Functions.Like(t.Address, "%" + suppilerVo.Address[0] + "/" + suppilerVo.Address[1] + "/" + suppilerVo.Address[2] + "%"))
                .Skip((page - 1) * pageSize).Take(pageSize).ToList();
            count = sql.Count(t => EF.Functions.Like(t.Address, "%" + suppilerVo.Address[0] + "/" + suppilerVo.Address[1] + "/" + suppilerVo.Address[2] + "%"));
        }
        else if (!string.IsNullOrWhiteSpace(suppilerVo.Phone) && suppilerVo.Address != null)
        {
            res = sql.Where(t => 
                    EF.Functions.Like(t.Address, "%" + suppilerVo.Address[0] + "/" + suppilerVo.Address[1] + "/" + suppilerVo.Address[2] + "%"))
                .Where(t => 
                    EF.Functions.Like(t.Phone, "%" + suppilerVo.Phone + "%"))
                .Skip((page - 1) * pageSize).Take(pageSize).ToList();
            count = sql.Where(t => 
                    EF.Functions.Like(t.Address, "%" + suppilerVo.Address[0] + "/" + suppilerVo.Address[1] + "/" + suppilerVo.Address[2] + "%"))
                .Count(t => EF.Functions.Like(t.Phone, "%" + suppilerVo.Phone + "%"));
        }
        else
        {
            res = sql.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            count = sql.Count();
        }

        var config = new MapperConfiguration(cfg => cfg.CreateMap<Suppiler, SuppilerVo>()
            .AfterMap((src, des) => des.Address = new []{src.Address.Split("/")[0], src.Address.Split("/")[1], src.Address.Split("/")[2]} )
            .ForMember(dest => dest.Address, opt => opt.Ignore())
        );
        var mapper = config.CreateMapper();
        return new PageResult<SuppilerVo>()
        {
            Items = res.Select(item => mapper.Map<SuppilerVo>(item)).ToList(),
            Total = count
        };
    }

    public List<SuppilerVo> GetSuppilerWithList()
    {
        var res = new List<Suppiler>();
        var sql = _defaultDbContext.Suppilers.AsQueryable();
        res = sql.ToList();
        var config = new MapperConfiguration(cfg => cfg.CreateMap<Suppiler, SuppilerVo>()
            .AfterMap((src, des) => des.Address = new []{src.Address.Split("/")[0], src.Address.Split("/")[1], src.Address.Split("/")[2]} )
            .ForMember(dest => dest.Address, opt => opt.Ignore())
        );
        var mapper = config.CreateMapper();
        return res.Select(item => mapper.Map<SuppilerVo>(item)).ToList();
    }
}